﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class trangchu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected override void InitializeCulture()
    {
        base.InitializeCulture();
        if (Session["lang"] != null)
        {
            Culture = Session["lang"].ToString();
            UICulture = Session["lang"].ToString();
        }
        else
        {
            Culture = "vi-VN";
            UICulture = "vi-VN";
        }
    }

    protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (String.Equals(e.CommandName, "AddToCard"))
        {
            
          
            ListViewDataItem dataItem = (ListViewDataItem)e.Item;
            TextBox tb = (TextBox)e.Item.FindControl("TextBox1");
            string sl = tb.Text;

            int soluong = System.Int32.Parse(sl);

            string IDTaiLieu =
              ListView1.DataKeys[dataItem.DisplayIndex].Value.ToString();
            string ten = ListView1.DataKeys[dataItem.DisplayIndex].Values["TenTL"].ToString();
            string gia = ListView1.DataKeys[dataItem.DisplayIndex].Values["DonGia"].ToString();
            int dongia;
            if (gia.Contains("."))
                dongia = System.Int32.Parse(gia.Replace(".", ""));
            else
                dongia = System.Int32.Parse(gia);
            int ID = System.Int32.Parse(IDTaiLieu);
            long thanh_tien =(long) dongia * soluong;
            //ShoppingCart tl = new ShoppingCart(ID,ten,dongia,soluong,thanh_tien);

            //Session.Add("gio_hang",tl);

            ShoppingCart card = (ShoppingCart)Session["gio_hang"];
            if (card == null)
                card = new ShoppingCart();
            card.Add(ID, ten, dongia, soluong, thanh_tien);
            Session["gio_hang"] = card;

            ASP.masterpage_master master = (ASP.masterpage_master)this.Master;
            master.RefreshShoppingCartInfo();
            Session["msg"] = "Thành công! Đã thêm sản phẩm vào giỏ hàng!";
            Session["msg_type"] = "success";
            /*
            if (SelectedEmployeesListBox.Items.FindByValue(IDTaiLieu) == null)
            {
                ListItem item = new ListItem(e.CommandArgument.ToString(), IDTaiLieu);
                SelectedEmployeesListBox.Items.Add(item);
            }
             */
        }
    }

    public string format_tt(string tt)
    {
        
        string rs = tt;
        //thay dau ',' thanh '.'
        int l = (rs.Length - 1) / 3;
        for (int i = 0; i < l; i++)
        {
            rs = rs.Insert(rs.Length - (i + 1) * 3 - i, ".");
        }
       
        return rs;
    }
}
